package nieAS.display {
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.utils.ByteArray;
	import nieAS.utils.ObjectUtils;
	
	public class MovieClipPage extends MovieClip {
		private static var pageList:Vector.<MovieClipPage> = new Vector.<MovieClipPage>();
		private var _isShow:Boolean = false;
		
		protected var _autoPlay:Boolean = false;
		protected var _parent:DisplayObjectContainer;
		protected var _ID:String;
		
		public static var currentPage:String = null;
		
		private var _bitmap:Bitmap;
		private var _bd:BitmapData;
		private var _renderTarget:DisplayObject;
		private var _bitmapList:Vector.<BitmapData> = new Vector.<BitmapData>();
		private var _renderCurrent:int = 0;
		private var _renderMatrix:Matrix;
		
		protected var _renderNumber:int = 10;
		protected var _renderWidth:int = 1680;
		protected var _renderHeight:int = 1000;
		
		public function MovieClipPage():void {
			_ID = ObjectUtils.getObjectName(this.toString());
			
			if (_parent == null) {
				_parent = this.parent;
			}
			
			this.addEventListener(Event.ADDED_TO_STAGE, _addToStage);
			this.addEventListener(Event.REMOVED_FROM_STAGE, _removeStage);
			
			pageList.push(this);
			
			if (_autoPlay) {
				_show(this._ID);
			}
		}
		
		protected function _addToStage(evt:Event):void {
			if (!_isShow) {
				_remove();
			}
		}
		
		protected function _removeStage(evt:Event):void {
			_remove();
		}
		
		protected function startRender(_target:DisplayObject, _targetWidth:int = -1):void {
			if (_renderTarget == null) {
				_renderWidth = root.stage.stageWidth > 1680 ? 1680 : root.stage.stageWidth;
				_bd = new BitmapData(_renderWidth, _renderHeight, false);
				_bitmap = new Bitmap(_bd);
				_bitmap.x = _target.x + ((_targetWidth == -1 ? _target.width : _targetWidth) - _renderWidth) / 2;
				_bitmap.y = _target.y;
				_renderMatrix = new Matrix(1, 0, 0, 1, -((_targetWidth == -1 ? _target.width : _targetWidth) - _renderWidth) / 2, 0);
			}
			_renderTarget = _target;
			_target.parent.addChild(_bitmap);
			_target.parent.swapChildren(_bitmap, _target);
			_renderTarget.addEventListener(Event.ENTER_FRAME, _render);
			_renderCurrent = 0;
			_render(null);
			_target.visible = false;
			_target.parent.removeChild(_target);
		}
		
		private function stopRender():void {
			_renderTarget.removeEventListener(Event.ENTER_FRAME, _render);
		}
		
		private function _render(evt:Event):void {
			if (_bitmapList.length >= _renderNumber) {
				_bd.lock();
				_bd.copyPixels(_bitmapList[_renderCurrent], _bd.rect, new Point(0, 0));
				_renderCurrent++;
				if (_renderCurrent > _renderNumber - 1) {
					_renderCurrent = 0;
				}
				_bd.unlock();
			} else {
				_bd.lock();
				_bd.draw(_renderTarget, _renderMatrix);
				_bitmapList.push(_bd.clone());
				_bd.unlock();
			}
		}
		
		protected static function _show(_id:String):Boolean {
			trace("show:" + _id);
			for (var i:int = 0; i < pageList.length; i++) {
				if (pageList[i]._ID == _id) {
					pageList[i]._isShow = true;
					pageList[i].visible = true;
					pageList[i].gotoAndPlay(1);
					currentPage = _id;
					return true;
				}
			}
			return false;
		}
		
		protected static function get current():MovieClipPage {
			for (var i:int = 0; i < pageList.length; i++) {
				if (pageList[i]._ID == currentPage) {
					return pageList[i];
				}
			}
			return null;
		}
		
		protected static function _hide(_id:String = ""):Boolean {
			if (_id == "") {
				for (var i:int = 0; i < pageList.length; i++) {
					if (pageList[i]._isShow == true) {
						pageList[i]._remove();
						currentPage = "";
						trace("hide:" + pageList[i]._ID);
						return true;
					}
				}
			} else {
				for (i = 0; i < pageList.length; i++) {
					if (pageList[i]._ID == _id) {
						pageList[i]._remove();
						currentPage = "";
						trace("hide:" + pageList[i]._ID);
						return true;
					}
				}
			}
			return false;
		}
		
		private function _remove():void {
			this._isShow = false;
			this.visible = false;
			this.stop();
			if (_renderTarget != null) {
				stopRender();
			}
		}
	}
}